package day20;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import org.junit.Test;

import day19.JDBCUtil;

public class ShijianTest {
	@Test
	public void transactionDemo() {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		String sql1 = "UPDATE account SET money=money-1000 WHERE NAME='张三';";
		String sql2 = "UPDATE account SET money=money+1000 WHERE NAME='李四';";
		//String sql3 = "SELECT * FROM account;";
		try {
			connection = JDBCUtil.getConnection();
			connection.setAutoCommit(false);
			preparedStatement = connection.prepareStatement(sql1);
			preparedStatement.executeUpdate();
			preparedStatement = connection.prepareStatement(sql2);
			preparedStatement.executeUpdate();
//			preparedStatement = connection.prepareStatement(sql3);
//			preparedStatement.executeQuery();
			connection.commit();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			connection.rollback();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
			JDBCUtil.close(connection, preparedStatement, null);
		}
	}
}
